/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.building;

import cz.fi.muni.diplomka.common.Coordinates;
import cz.fi.muni.diplomka.common.enumeration.BuildingType;
import cz.muni.fi.diplomka.servicelayer.dto.BuildingInCity;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityBuildingManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityBuildingManagerImpl;
import cz.muni.fi.diplomka.servicelayer.xmlconvertors.BuildingConvertor;
import cz.muni.fi.diplomka.webapp.layout.EmptyPanel;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.FeedbackPanel;

/**
 *
 * @author Jakub Kutil
 */
public class BuildingPage extends WebPage {

    private CityBuildingManager cityBuildingManager = new CityBuildingManagerImpl();
    private BuildingConvertor buildingConvertor = new BuildingConvertor();

    public BuildingPage(final PageParameters parameters) {
        add(new FeedbackPanel("feedback"));

        Long id = new Long(0);
        Long cityId = new Long(0);
        int x = 0;
        int y = 0;

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));
            PageParameters p = new PageParameters();
            p.add("id", parameters.getString("id"));
            add(new LinkPanel("linkPanel", p));
        }

        if (parameters.containsKey("x")) {
            x = Integer.parseInt(parameters.getString("x"));
        }

        if (parameters.containsKey("y")) {
            y = Integer.parseInt(parameters.getString("y"));
        }
        if (parameters.containsKey("cityId")) {
            cityId = parameters.getAsLong("cityId");

        }

        Coordinates c = new Coordinates();
        c.setX_coordinate(x);
        c.setY_coordinate(y);

        BuildingInCity b = cityBuildingManager.getBuildingAt(cityId, c);
        BuildingInCity building = buildingConvertor.getBuildingFromXML(b.getName().toString());
        building.setCityId(cityId);
        building.setLevel(b.getLevel());
        building.setCoordinates(b.getCoordinates());
        building.setName(b.getName());


        add(new Label("nameLabel", building.getName().toString() + " at [" + building.getCoordinates().getX_coordinate() + ","
                + building.getCoordinates().getY_coordinate() + "]"));

        if (building.getName() != BuildingType.Empty) {
            add(new Label("levelLabel", "Level: " + building.getLevel()));
        } else {
            add(new Label("levelLabel", ""));
        }

        if (building.getName() == BuildingType.Barracks || building.getName() == BuildingType.Walls
                || building.getName() == BuildingType.Empty || building.getName() == BuildingType.Castle) {
            add(new EmptyPanel("razePanel"));
        } else {
            add(new RazePanel("razePanel", parameters));
        }
        if (building.getName() == BuildingType.Empty) {
            if (cityBuildingManager.getBuildingInProgressAtPosition(cityId, c).getCityId() != null) {
                add(new EmptyPanel("buildPanel"));
                add(new Label("building", cityBuildingManager.getBuildingInProgressAtPosition(cityId, c).getName()
                        + " is being built here." + "Time of finish: "
                        + cityBuildingManager.getBuildingInProgressAtPosition(cityId, c).getTimeOfFinish()));
            } else {
                add(new Label("building", ""));
                add(new BuildPanel("buildPanel", parameters));
            }
        } else {
            add(new EmptyPanel("buildPanel"));
            add(new Label("building", ""));
        }

        if (building.getLevel() < 10 && building.getName() != BuildingType.Empty) {
            parameters.add("name", building.getName().toString());
            parameters.add("level", building.getLevel() + "");
            add(new UpgradePanel("upgradePanel", parameters));
        } else {
            add(new EmptyPanel("upgradePanel"));
        }
    }
}
